package com.hyb.hybmodulesblog.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.RemoteSearchService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hyb.hybmodulesblog.domain.Tribe;
import com.hyb.hybmodulesblog.service.ITribeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 部落表增删改查Controller
 * 
 * @author hyb
 * @date 2023-03-11
 */
@RestController
@RequestMapping("/tribe")
public class TribeController extends BaseController
{
    @Autowired
    private ITribeService tribeService;

    @Autowired
    private RemoteSearchService searchService;


    @GetMapping("/getTribe")
    public AjaxResult getTribe(@RequestParam("id")Long id){
        return AjaxResult.success().put("tribe",tribeService.selectTribeById(id));
    }

    /**
     * 查询部落表增删改查列表
     */
    @RequiresPermissions("tribe:tribe:list")
    @GetMapping("/list")
    public TableDataInfo list(Tribe tribe)
    {
        startPage();
        List<Tribe> list = tribeService.selectTribeList(tribe);
        return getDataTable(list);
    }

    /**
     * 导出部落表增删改查列表
     */
    @RequiresPermissions("tribe:tribe:export")
    @Log(title = "部落表增删改查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tribe tribe)
    {
        List<Tribe> list = tribeService.selectTribeList(tribe);
        ExcelUtil<Tribe> util = new ExcelUtil<Tribe>(Tribe.class);
        util.exportExcel(response, list, "部落表增删改查数据");
    }

    /**
     * 获取部落表增删改查详细信息
     */
    @RequiresPermissions("tribe:tribe:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tribeService.selectTribeById(id));
    }

    /**
     * 新增部落表增删改查
     */
    @RequiresPermissions("tribe:tribe:add")
    @Log(title = "部落表增删改查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tribe tribe)
    {
        tribe.setNumber(com.ruoyi.common.utils.uuid.VerificationCode.getNumber(9));
        tribeService.insertTribe(tribe);
        com.ruoyi.system.api.domain.Tribe t = new com.ruoyi.system.api.domain.Tribe();
        BeanUtils.copyProperties(tribe,t);
        tribe.setUpdateTime(DateUtils.getNowDate());
        searchService.insert(t, SecurityConstants.INNER);
        return AjaxResult.success();
    }

    /**
     * 修改部落表增删改查
     */
    @RequiresPermissions("tribe:tribe:edit")
    @Log(title = "部落表增删改查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tribe tribe)
    {

        tribe.setNumber(null);
        tribeService.updateTribe(tribe);
        com.ruoyi.system.api.domain.Tribe t = new com.ruoyi.system.api.domain.Tribe();
        BeanUtils.copyProperties(tribe,t);
        tribe.setUpdateTime(DateUtils.getNowDate());
        searchService.update(t,SecurityConstants.INNER);
        return AjaxResult.success();
    }

    /**
     * 删除部落表增删改查
     */
    @RequiresPermissions("tribe:tribe:remove")
    @Log(title = "部落表增删改查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tribeService.deleteTribeByIds(ids));
    }
}
